<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;


/**
 * App\Models\Item
 *
 * @property int $itemid 商品ID
 * @property int $uid 用户ID
 * @property int $catid 宝贝分类
 * @property int $shop_id 店铺ID
 * @property string $title 宝贝标题
 * @property string $subtitle 宝贝卖点
 * @property string $sn 商品编号
 * @property string $thumb 商品缩略图
 * @property string $image 商品图片
 * @property float $price 商品价格
 * @property float $market_price 市场价格
 * @property int $isdiscount 是否有折扣
 * @property int $on_sale 出售状态,1=出售中,0=已下架
 * @property int $is_best 仓储推荐
 * @property int $stock 库存
 * @property int $sold 累计销量
 * @property int $views 浏览量
 * @property int $collections 收藏数量
 * @property int $reviews 评论数
 * @property float $shipping_fee 运费
 * @property string $unit
 * @property int $created_at
 * @property int $updated_at
 * @property-read \App\Models\ItemCatlog $catlog
 * @property-read \App\Models\ItemContent $content
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\ItemImage[] $images
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\ItemProps[] $props
 * @property-read \App\Models\Shop $shop
 * @property-read \App\Models\User $user
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Item offSale()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Item onSale()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Item whereCatid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Item whereCollections($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Item whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Item whereImage($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Item whereIsBest($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Item whereIsdiscount($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Item whereItemid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Item whereMarketPrice($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Item whereOnSale($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Item wherePrice($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Item whereReviews($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Item whereShippingFee($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Item whereShopId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Item whereSn($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Item whereSold($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Item whereStock($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Item whereSubtitle($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Item whereThumb($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Item whereTitle($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Item whereUid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Item whereUnit($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Item whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Item whereViews($value)
 * @mixin \Eloquent
 * @property string|null $poster 海报图片
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\ItemReviews[] $buyerReviews
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Item wherePoster($value)
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\ItemSkuAttr[] $skuAttrs
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\ItemSkuAttr[] $skuValues
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\ItemSku[] $skus
 */
class Item extends Model
{
    protected $table = 'item';
    protected $primaryKey = 'itemid';
    protected $guarded    = ['itemid'];

    public $timestamps = false;

    public static function boot()
    {
        parent::boot(); // TODO: Change the autogenerated stub

        static::creating(function ($model){
            $model->created_at = time();
            $model->updated_at = time();
        });

        static::updating(function ($model){
            $model->updated_at = time();
        });

        static::deleted(function (Item $item){
            $item->images()->delete();
            $item->content()->delete();
            $item->props()->delete();
        });
    }

    public function incrementSold($amount = 1)
    {
        return $this->increment('sold', $amount);
    }

    public function incrementViews($amount = 1)
    {
        return $this->increment('views', $amount);
    }

    public function incrementCollections($amount = 1)
    {
        return $this->increment('collections', $amount);
    }

    public function decrementStock($amount = 1)
    {
        return $this->decrement('stock', $amount);
    }

    /**
     * 出售中
     * @param $query
     * @return mixed
     */
    public function scopeOnSale($query) {
        return $query->where('on_sale', 1);
    }

    /**
     * 已下架
     * @param $query
     * @return mixed
     */
    public function scopeOffSale($query) {
        return $query->where('on_sale', 0);
    }

    public function content()
    {
        return $this->hasOne(ItemContent::class, 'itemid');
    }

    public function images()
    {
        return $this->hasMany(ItemImage::class, 'itemid');
    }

    public function props()
    {
        return $this->hasMany(ItemProps::class, 'itemid');
    }

    public function catlog()
    {
        return $this->belongsTo(ItemCatlog::class, 'catid', 'catid');
    }

    public function shop()
    {
        return $this->belongsTo(Shop::class, 'shop_id', 'shop_id');
    }

    public function user()
    {
        return $this->belongsTo(User::class, 'uid', 'uid');
    }

    public function buyerReviews()
    {
        return $this->hasMany(ItemReviews::class, 'itemid', 'itemid');
    }

    public function skuAttrs()
    {
        return $this->hasMany(ItemSkuAttr::class, 'itemid', 'itemid');
    }

    public function skuValues()
    {
        return $this->hasMany(ItemSkuAttr::class, 'itemid', 'itemid');
    }

    public function skus()
    {
        return $this->hasMany(ItemSku::class, 'itemid', 'itemid');
    }
}
